<?php

namespace Biz\service\activity\project;

use Biz\cache\activity\CBizProject;
use Biz\model\activity\BizProjectBaseModel;
use Biz\model\activity\BizProjectCustomerModel;
use Biz\model\activity\BizProjectMcnModel;
use Biz\model\activity\BizProjectModel;
use Biz\model\activity\BizProjectResponsibilityModel;
use Doraemon\model\Admin\BeerUsUser;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;
use Hhz\AdministrativeRegion\Region;

class BizProjectMcn extends BizProjectPageTpl
{

    const TPL_CONFIG_TPL_NAME = '达人合作';

    public function getTotal(): int
    {
        return BizProjectMcnModel::statusTotal($this->getRequestCondition());
    }

    /**
     * @throws Exception
     */
    public function getList(): array
    {
        $this->authorError();
        $list = BizProjectMcnModel::dataSelect( BizProjectMcnModel::statusCondition() + $this->getRequestCondition() + ['ORDER' => ['id' => 'DESC'], 'LIMIT' => [$this->getOffset(), $this->getPageSize()]]);
        if (empty($list)) return [];
        $uids = array_column($list, 'uid');
        $profiles = \Doraemon\service\UserPortrait\UserPortraitApi::request($uids, ['fans_num','product_level','nick','user_business_layer']);
        foreach ($list as &$v) {
            $profile = $profiles[$v['uid']] ?? [];
            $v += [
                'user_nick' => $profile['nick'] ?? '-',
                'user_fans' => $profile['fans_num'] ?? '-',
                'user_level' => $profile['product_level'] ?? '-',
                'user_brand_level' => $profile['user_business_layer'] ?? '-',
                'updated_at_date' => $v['created_at'],
            ];
        }
        return $list;
    }

    public function condition(): array
    {
        return ['project_id' => $this->getPid()];
    }

    public function withInfoHtml(): array
    {
        $info = BizProjectModel::getAdmInfo($this->getPid());
        $projectInfo = $info['project'] ?? [];
        $html = [
            "项目编号 : " . ($projectInfo['no'] ?? ''),
            "项目名称 : " . ($projectInfo['name'] ?? ''),
        ];

        return ["项目信息", implode("<br/>", $html)];
    }

    public function getGrid(Grid $grid)
    {
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->column('id', '客户ID');
        $grid->column('uid', 'uid');
        $grid->column('user_nick', '昵称');
        $grid->column('user_fans', '粉丝数');
        $grid->column('user_level', '创作者等级');
        $grid->column('user_brand_level', '商业等级');
        $grid->column('publish_time', '发布时间');
        $grid->column('publish_link', '发布链接');
        $grid->column('cost', '稿费成本');
        $grid->column('other_cost', '其他成本');
        $grid->column('submit_time', '提报业绩结算时间');
        $grid->column('submit_time', '执行业绩结算时间');
        $grid->column('kol_time', 'KOL结算时间');
        $grid->column('updated_at_date', '创建时间');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
    }

    public function getForm(Form $form)
    {
        $form->confirm('确定提交吗？');
        $form->text('uid', 'uid')->attribute('maxlength', '50')->required();
        $form->date('publish_time', '发布时间');
        $form->text('publish_link', '发布链接');
        $form->text('cost', '稿费成本');
        $form->text('other_cost', '其他成本');
        $form->date('submit_time', '提报业绩结算时间');
        $form->date('exec_time', '执行业绩结算时间');
        $form->date('kol_time', 'KOL结算时间');
        $form->disableReset();
    }

    public function getFormInfo($id)
    {
        $info = $id ? BizProjectMcnModel::get('*', ['id' => $id]) : [];
        if ($info) {

        }
        return $info;
    }

    /**
     * @throws Exception
     */
    public function save($input)
    {
        $this->authorError();
        $uid = $input['uid'] ?? '';
        $publish_time = $input['publish_time'] ?? '';
        $publish_link = $input['publish_link'] ?? '';
        $cost = $input['cost'] ?? '';
        $other_cost = $input['other_cost'] ?? '';
        $submit_time = $input['submit_time'] ?? '';
        $exec_time = $input['exec_time'] ?? '';
        $kol_time = $input['kol_time'] ?? '';
        $id = $input['id'] ?? 0;
        $project_id = $this->getPid();
        if (empty($uid)) throw new Exception("uid必填");
        $addData = compact('uid', 'publish_time', 'publish_link', 'cost', 'other_cost', 'id', 'submit_time', 'exec_time', 'kol_time', 'project_id');
        BizProjectMcnModel::addData($addData);
    }

    /**
     * @throws Exception
     */
    public function delete($id)
    {
        $this->authorError();
        BizProjectMcnModel::statusDelete($id);
    }

    public function breadcrumb(): array
    {
        return [
            [
                'text' => BizProjectProject::getListTitle(), 'url' => BizProjectProject::getRoute()->getEditRoute($this->getPid())
            ],
        ];
    }
    /**
     * @throws Exception
     */
    public function authorError()
    {
        $auth = BizProjectGroup::fieldAuth($this->adminId(), 'mcn');
        $auth = $auth['mcn'] ?? 0;
        if (!$this->saveForm($auth)) throw new Exception("权限不足");
    }

}